package com.zxy.day0624_util.utils;


import java.math.BigDecimal;
import java.math.RoundingMode;
/**
 * @Auther: lenovo
 * @Date: 2021/9/27 20:51
 * @Description:
 */
public class NumUtil {
    private static String NUMBER_REG = "^\\d+$";
    private static String REAL_REG = "^(-)?[0-9]+(\\.[0-9]+)?$";

    public NumUtil() {
    }

    public static boolean isNum(String param) {
        return !StringUtil.hasText(param) ? false : param.matches(NUMBER_REG);
    }

    public static boolean isReal(String param) {
        return !StringUtil.hasText(param) ? false : param.matches(REAL_REG);
    }

    public static double add(double v1, double v2) {
        BigDecimal bd1 = new BigDecimal(v1 + "");
        BigDecimal bd2 = new BigDecimal(v2 + "");
        return bd1.add(bd2).doubleValue();
    }

    public static double sub(double v1, double v2) {
        BigDecimal bd1 = new BigDecimal(v1 + "");
        BigDecimal bd2 = new BigDecimal(v2 + "");
        return bd1.subtract(bd2).doubleValue();
    }

    public static double mul(double v1, double v2) {
        BigDecimal bd1 = new BigDecimal(v1 + "");
        BigDecimal bd2 = new BigDecimal(v2 + "");
        return bd1.multiply(bd2).doubleValue();
    }

    public static double div(double v1, double v2, int scale) {
        BigDecimal bd1 = new BigDecimal(v1 + "");
        BigDecimal bd2 = new BigDecimal(v2 + "");
        return bd1.divide(bd2, scale, RoundingMode.HALF_UP).doubleValue();
    }

    public static double round(double v, int scale) {
        BigDecimal bd = new BigDecimal(v + "");
        return bd.setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }
}
